package com.teamclub.domain.erp.storage;

import com.teamclub.domain.system.AppDomainInstance;
import com.teamclub.util.libs.Libs;
import io.ebean.Ebean;
import io.ebean.Model;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.Date;

/**
 * Created by ilkkzm on 17-9-11.
 */

@Entity
@Table(name="erp_storage_record")
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class StorageRecord extends Model{
    @Id
    private Integer id;
    private String producerOrganNo;
    private String dealerOrganNo;
    private String batchNo;
    private Integer storageId; //　关联仓库
    private Integer recordType; // '1 入库　２　出库　３退货'
    private String linkCustomer; //关联客户
    private String linkOrderNo;
    private Integer operatorId; //操作人id
    private Date actionTime; //入库/出库/退货的日期
    private Date createTime;

    @Transient
    private String producerName;
    @Transient
    private String formattedActionTime;
    @Transient
    private String storageName;
    @Transient
    private String recordTypeName;
    @Transient
    private String organName;

    public String getProducerName() {
        if(StringUtils.isBlank(this.producerOrganNo)) return "";
        AppDomainInstance instance = Ebean.find(AppDomainInstance.class).where().eq("organNo", this.producerOrganNo).setMaxRows(1).findOne();
        return instance == null ? "": instance.getName();
    }

    public String getRecordTypeName() {
        if(this.recordType == 1) {
            return "入库";
        } else if (this.recordType == 2) {
            return "出库";
        } else if (this.recordType == 3) {
            return "退货";
        }
        return "-";
    }

    public String getStorageName() {
        if(this.storageId == null || this.storageId == 0) {
            return "默认仓库";
        }
        StorageInfo instance = Ebean.find(StorageInfo.class, this.storageId);
        return instance == null ? "" : instance.getStorageName();
    }

    public String getFormattedActionTime() {
        if(this.getActionTime() != null) {
            return Libs.parseDate(this.getActionTime());
        } else {
            return "";
        }
    }
}
